window.onload = function () {
    getAllDormitory();
    //getAllDept();
    //getMajorByDeptId();
    //getClazzByMajorId();
}

var layer, table;
layui.use(['layer', 'laydate', 'table'], function () {
    layer = layui.layer;
    table = layui.table;
});

/**
 * 三级联动，获取学生信息并自动填充
 */
function getStuInfo() {
    // 获取表单参数
    var stuId = $("#stuId").val();
    var param = {
        stuId: stuId
    }
    if (!$.isEmptyObject(param.stuId)) {
        $.ajax({
            type: "POST",
            url: "/student/getStuInfoById",
            //contentType: "application/json",
            sync: true,
            data: param,
            dataType: "json",
            success: function (result) {
                console.log(result)
                if (result.code == 200) {
                    var stu = result.data;
                    $("#stuName").val(stu.stuName);
                    getAllDormitory(stu.dormitoryId)
                    // getAllDept(stu.deptId);
                    // getMajorByDeptId(stu.majorId);
                    // getClazzByMajorId(stu.clazzId);
                } else {
                    layer.msg('该学生不存在！', {icon: 2, offset: '100px', time: 2000});
                }
            }
        });
    }
}

/**
 * 获取所有宿舍
 * @param dormitoryId
 */
function getAllDormitory(dormitoryId) {
    param = {
        dormitoryId: ""
    }
    $.ajax({
        type: "POST",
        url: "/dormitory/getAllDormitory",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#dormitory").empty();
            $("#dormitory").append("<option value=''>请选择宿舍</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (dormitoryId === o.dormitoryId) {
                        opt = '<option value=' + o.dormitoryId + ' selected>' + o.dormitoryName + '</option>';
                    } else {
                        opt = '<option value=' + o.dormitoryId + '>' + o.dormitoryName + '</option>';
                    }
                    $("#dormitory").append(opt);
                });
            }
            $("#dormitory").selectpicker('refresh');//动态加载
        }
    });
}

/**
 * 获取所有院系
 * @param deptId
 */
function getAllDept(deptId) {
    $.ajax({
        type: "POST",
        url: "/common/getAllDept",
        contentType: "application/json",
        sync: true,
        data: {},
        dataType: "json",
        success: function (result) {
            $("#department").empty();
            $("#department").append("<option value=''>请选择院系</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (deptId === o.deptId) {
                        opt = '<option value=' + o.deptId + ' selected>' + o.deptName + '</option>';
                    } else {
                        opt = '<option value=' + o.deptId + '>' + o.deptName + '</option>';
                    }
                    $("#department").append(opt);
                });
            }
            $("#department").selectpicker('refresh');//动态加载
        }
    });
}

/**
 * 根据院系获取专业
 * @param majorId
 */
function getMajorByDeptId(majorId) {
    // 获取表单参数
    var deptId = $("#department option:selected").val();
    var param = {
        deptId: deptId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getMajorByDeptId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#major").empty();
            $("#major").append("<option value=''>请选择专业</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (majorId === o.majorId) {
                        opt = '<option value=' + o.majorId + ' selected>' + o.majorName + '</option>';
                    } else {
                        opt = '<option value=' + o.majorId + '>' + o.majorName + '</option>';
                    }
                    $("#major").append(opt);
                });
            }
            $("#major").selectpicker('refresh');//动态加载
        }
    });
}

/**
 * 根据专业获取班级
 * @param clazzId
 */
function getClazzByMajorId(clazzId) {
    // 获取表单参数
    var majorId = $("#major").val();
    var param = {
        majorId: majorId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getClazzByMajorId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#clazz").empty();
            $("#clazz").append("<option value=''>请选择班级</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '';
                    if (clazzId === o.clazzId) {
                        opt = '<option value=' + o.clazzId + ' selected>' + o.clazzName + '</option>';
                    } else {
                        opt = '<option value=' + o.clazzId + '>' + o.clazzName + '</option>';
                    }
                    $("#clazz").append(opt);
                });

            }
            $("#clazz").selectpicker('refresh');//动态加载
        }
    });
}

/**
 * 访问登记
 */
function visitRegister() {
    var stuId = $("#stuId").val();
    var stuName = $("#stuName").val();
    var dormitoryId = $("#dormitory").val();
    var stuTemp = $("#stuTemp").val();

    var param = {
        stuId: stuId,
        stuName: stuName,
        dormitoryId: dormitoryId,
        stuTemp: stuTemp
    }

    if (verify(param)) {
        $.ajax({
            type: "POST",
            url: "/temp/visitRegister",
            contentType: "application/json",
            sync: true,
            data: JSON.stringify(param),
            dataType: "json",
            success: function (result) {
                if (result.code == 200) {
                    layer.msg('登记成功！', {icon: 1, offset: '100px', time: 2000});
                    setTimeout(() => {
                        window.location.href = "index";
                    }, 2000);
                } else {
                    layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
                }
            }
        });
    }
}

/**
 * 校验表单
 * @param param
 * @returns {boolean}
 */
function verify(param) {
    var stuId = param.stuId;
    var stuName = param.stuName;
    var dormitoryId = param.dormitoryId;
    var stuTemp = param.stuTemp;

    if ($.isEmptyObject(stuId)) {
        layer.msg('请填写访问学生学号！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(stuName)) {
        layer.msg('请填写访问学生姓名！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(dormitoryId)) {
        layer.msg('请选择访问宿舍！', {icon: 2, offset: '100px', time: 2000});
        return false;
    } else if ($.isEmptyObject(stuTemp)) {
        layer.msg('请填写访问者体温！', {icon: 2, offset: '100px', time: 2000});
        return false;
    }
    return true;
}